home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Command line Arguments
- Date: 6 Feb 1996 04:49:30 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4f6mkq$2f7@news.iag.net>
- References: <4f2qev$9jq@cloner3.netcom.com> <4f60cr$34v@jaxnet.jaxnet.com>
- NNTP-Posting-Host: pm4-orl9.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4f60cr$34v@jaxnet.jaxnet.com>, garyg@jax.jaxnet.com says...
- >
- >Glen 'Steve' Vandiver (buxx@ix.netcom.com) wrote:
- >: Hi! I have been haveing trouble with my commandline arguements. I have
- >: it too where i can read the entire argument string after the run. No
- >: prob. but lets say i want it to split it up like this. the first word
- >: goes into char *user; and the rest goes into char *command;. HOW WOULD
- >: I DO THIS? thanx! bye!
- >
- >I think this is what you mean:
- >
- >#include <stdio.h>
- >#include <string.h>
-
- #include <stdlib.h> /* for exit */
-
- >#define LEN 256
- >int main (int argc,char **argv)
- >{
- >
- > char user[LEN], command[LEN*4];
-
- char user[LEN], command[LEN*4] = ""; /* initialization required */
-
-
- > int i;
- > if(argc<3) {
- > printf("Enter more command line args. Bye\n");
- > exit (1);
-
- exit( EXIT_FAILURE); /* this is more portable */
-
- > }
- > strcpy(user,argv[1]);
- > for(i=2;i<argc;i++)
- > strcat(command,argv[i]);
-
- If you don't initialize command to an empty string before the first strcat,
- your results will be a bit unpredictable.
-
- I believe that this is os dependent, but I suspect that on most systems
- this will cause the args to blur together (ie there will be no spaces
- serarating them). You might want to modify this a bit. How about:
-
- /* remember to add definitions for currEOS and numChrs */
- for(i=2;i<argc;i++)
- {
- /* you may want to add a test here to protect command's bounds */
- sprintf(command + currEOS, "%s %n", argv[i], &numChrs);
- currEOS += numChrs;
- }
-
-
- > printf("User: %s\tCommand: %s\n",user,command);
- > return 0;
- >}
- >
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-